package com.fyzk.activity.myview;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class BeiShuEditText extends AppCompatEditText{
	private Context context;
	
	public BeiShuEditText(Context context) {
//		super(context);
		this(context, null);
	}

	public BeiShuEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		addTextChangedListener(new ET_TextWatcher());
	}
	
	@Override
	public void addTextChangedListener(TextWatcher watcher) {
		// TODO Auto-generated method stub
		super.addTextChangedListener(watcher);
	}
	
	private EditText getEditText(){
		return this;
	}
	
	public static interface ET_action{
		void action();

		View initCenterView();
	}
	
	private ET_action et;
	public void setEt(ET_action et){
		this.et = et;
	}
	
	
	/**
	 *  追期、投倍  增减 方法   
	 * @param zhuiOrTou   0 表示 追期， 1 表示  投倍  ；
	 * @param leftOrRight   0 表示  减少（Left）， 1 表示  增加（Right）
	 * @return
	 */
	public String zhui_tou(int zhuiOrTou,int leftOrRight){
		int textZhi = 0;
		if (zhuiOrTou == 0) { // 追期
			if (!getEditText().getText().toString().equals("")) {
				try {
					textZhi = Integer.parseInt(getEditText().getText().toString());
				} catch (Exception e) {
				}
			}
			if (leftOrRight == 0) {  // 减少
				if (textZhi > 1) {
					textZhi--;
				}
			}else {  // 增加
				textZhi++;
			}
			
		}else{ // 投倍
			if (getEditText().getText() != null && !getEditText().getText().toString().equals("")) {
				try {
					
					textZhi = Integer.parseInt(getEditText().getText()+"");
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			if (leftOrRight == 0) {  // 减少
				if (textZhi > 1) {
					textZhi--;
				}
			}else {    // 增加
				textZhi++;
				if (textZhi > 99) {
					Toast.makeText(context, "倍数最多为99倍", Toast.LENGTH_LONG).show();
					textZhi = 99;
				}
			}
		}
		
		return textZhi+"";
	}
	
	class ET_TextWatcher implements TextWatcher{

		private CharSequence temp ; 
        private boolean isEdit = true; 
        private int selectionStart ; 
        private int selectionEnd ; 
        @Override 
        public void beforeTextChanged(CharSequence s, int arg1, int arg2, 
                int arg3) {
            temp = s; 
        } 

        @Override 
        public void onTextChanged(CharSequence s, int arg1, int arg2, 
                int arg3) { 
        } 

        @Override 
        public void afterTextChanged(Editable s) { 
             selectionStart = getEditText().getSelectionStart(); 
            selectionEnd = getEditText().getSelectionEnd(); 
            if (temp .length() > 2) { 
            	Toast.makeText(context, "倍数最多为99倍", Toast.LENGTH_LONG).show();
                s.delete( selectionStart-1, selectionEnd); 
                getEditText().setText(s);
                getEditText().setText("99");
            }
            if (temp.length() > 1) {
				
            	int t = 0;
            	try {
            		t = Integer.parseInt(s.toString());
            		if (t<10) {
            			getEditText().setText(t+"");
					}
            	} catch (Exception e) {
            		// TODO: handle exception
            	}
			}
            if (temp.length() == 0) {
            	getEditText().setText("1");
			}
            
            getEditText().setSelection(temp.length());
            
//            bei = Integer.parseInt(getEditText().getText().toString());
            
			et.action();
        }

	}

}
